1 package mobisnap.mobile_trx;
2
3 import java.io.*;
4 import java.sql.*;
5 import java.util.*;
6 import mobisnap.*;
7 import nmp.dbms.*;
8 import nmp.dbms.JDBC.*;
9 import mobisnap.common.reservation.*;
10
11 /***
12 * Interprets MobisnapSQL
13 */
14 public class MobisnapInterpreter
15 {
16 protected Jdbc_Sql database;
17 protected TransactionHandle transaction;
18 protected MobisnapSQL interpreter;
19 /***
20 * Used to store the reservation used to process a mobile transaction during
21 * the course of its execution
22 */
23 protected Vector refRsrvs;
24 protected Vector usedRsrvs;
25
26 public MobisnapInterpreter( String driverName, String url, String name, String pwd ) throws SQLException {
27 database = new Jdbc_Sql( driverName, url, name, pwd);
28 transaction = database.getTransactionHandle();
29 refRsrvs = new Vector();
30 usedRsrvs = new Vector();
31 }
32
33 public MobisnapInterpreter( Jdbc_Sql database) throws SQLException {
34 this.database = database;
35 transaction = database.getTransactionHandle();
36 refRsrvs = new Vector();
37 usedRsrvs = new Vector();
38 }
39
40 /***
41 * Releases interpreter
42 */
43 public void release() throws SQLException {
44 if( transaction != null) {
45 transaction.release();
46 transaction = null;
47 }
48 if( database != null) {
49 database.closeAll();
50 database = null;
51 }
52 }
53
54 /***
55 * Initializes internal structures related with transaction processing
56 */
57 protected void initTransaction( Reader reader) throws Exception {
58 if( transaction == null)
59 throw new SQLException( "Interpreter not ready");
60 if( interpreter == null) {
61 MobisnapSQL.transaction = transaction;
62 transaction.commit();
63 MobisnapSQL.names = new MSQLNames();
64 interpreter = new MobisnapSQL( reader);
65 } else {
66 transaction.commit();
67 MobisnapSQL.names.reset();
68 MobisnapSQL.ReInit( reader);
69 }
70 }
71
72 /***
73 * Initializes internal structures related with query processing
74 */
75 protected void initQuery( Reader reader) throws Exception {
76 if( database == null)
77 throw new SQLException( "Interpreter not ready");
78 if( interpreter == null) {
79 MobisnapSQL.transaction = transaction;
80 transaction.commit();
81 MobisnapSQL.names = new MSQLNames();
82 interpreter = new MobisnapSQL( reader);
83 } else {
84 transaction.commit();
85 MobisnapSQL.names.reset();
86 MobisnapSQL.ReInit( reader);
87 }
88 }
89
90 /***
91 * interpreter should be properly initialized - defined only to
92 * make the definition visible to outside extensions
93 */
94 protected ASTDatabaseDefinition databaseDefinition() throws Exception {
95 return MobisnapSQL.DatabaseDefinition();
96 }
97
98 /***
99 * interpreter should be properly initialized - defined only to
100 * make the definition visible to outside extensions
101 */
102 protected ASTCreateSnapshot createSnapshot() throws Exception {
103 return MobisnapSQL.CreateSnapshot();
104 }
105
106 /***
107 * interpreter should be properly initialized - defined only to
108 * make the definition visible to outside extensions
109 */
110 protected ASTUpdateSnapshot updateSnapshot() throws Exception {
111 return MobisnapSQL.UpdateSnapshot();
112 }
113
114 /***
115 * interpreter should be properly initialized - defined only to
116 * make the definition visible to outside extensions
117 */
118 protected ASTGetReservation getReservation() throws Exception {
119 return MobisnapSQL.GetReservation();
120 }
121
122 /***
123 * interpreter should be properly initialized - defined only to
124 * make the definition visible to outside extensions
125 */
126 protected ASTCreateTable createTable( java.util.HashMap map) throws Exception {
127 return MobisnapSQL.CreateTable( map);
128 }
129
130 /***
131 * interpreter should be properly initialized - defined only to
132 * make the definition visible to outside extensions
133 */
134 protected ASTDropTable dropTable( java.util.HashMap map) throws Exception {
135 return MobisnapSQL.DropTable(map);
136 }
137
138 /***
139 * interpreter should be properly initialized - defined only to
140 * make the definition visible to outside extensions
141 */
142 protected ASTSQLStatement sqlStatement() throws Exception {
143 return MobisnapSQL.SQLStatement();
144 }
145
146 /***
147 * Process a query statement
148 */
149 public synchronized SQLVectorResult processQuery( int msql_type, Reader reader) throws Exception {
150 initQuery( reader);
151 ASTQueryStatement node = MobisnapSQL.QueryStatement();
152 return node.getResult( msql_type);
153 }
154
155 /***
156 * Initializes reservations
157 */
158 private void initReservations() {
159 refRsrvs.removeAllElements();
160 usedRsrvs.removeAllElements();
161 }
162
163 /***
164 * Should be called when some reservation has been associated with any variable
165 */
166 public void refReservation( ReservationUseBase rsrv) {
167 refRsrvs.addElement( rsrv);
168 }
169
170 /***
171 * Should be called when some reservation has effectively been used in the execution
172 * of the mobile transactions, i.e., when some test has been made that must be
173 * backed up by the transaction
174 */
175 public void useReservation( ReservationUseBase rsrv) {
176 usedRsrvs.addElement( rsrv);
177 }
178
179 /***
180 * The associated transaction has been commit - used reservations must be updated
181 */
182 protected void commitReservations() {
183 // do nothing
184 }
185
186 /***
187 * The associated reservations has been rollbacked - used reservations must be updated
188 * If called after "commitReservations" it has no effect
189 */
190 protected void rollbackReservations() {
191 // do nothing
192 }
193
194 /***
195 * Returns the used reservation that backs up the given conditions
196 *
197 * @param table Table name of possible reservation
198 * @param column Column name of possible reservation
199 * @param where Table row specification
200 * @return Returns null if no used reservation backs up the given conbditions
201 */
202 public ReservationUseBase getUsedReservation( String table, String column, SimpleNode where) {
203 where = where.simplify( MobisnapConstants.MSQL_SERVER, false);
204 Enumeration enum = usedRsrvs.elements();
205 while( enum.hasMoreElements()) {
206 ReservationUseBase rsrv = (ReservationUseBase)enum.nextElement();
207 if( rsrv.getRsrv().checkReservation( table, column, where))
208 return rsrv;
209 }
210 return null;
211 }
212
213 /***
214 * Returns the used reservation that backs up the given conditions
215 *
216 * @param table Table name of possible reservation
217 * @param column Column name of possible reservation
218 * @param conditions Table row specification
219 * @return Returns null if no used reservation backs up the given conbditions
220 */
221 public ReservationUseBase getUsedReservation( String table, String column, Vector conditions) {
222 Enumeration enum = usedRsrvs.elements();
223 while( enum.hasMoreElements()) {
224 ReservationUseBase rsrv = (ReservationUseBase)enum.nextElement();
225 if( rsrv.getRsrv().checkReservation( table, column, conditions))
226 return rsrv;
227 }
228 return null;
229 }
230
231
232 /***
233 * Process a mobile transaction
234 *
235 * @param msql_type Processing type (server, client tentative, client reservation)
236 * @param reader Reader to read mobile transaction
237 */
238 public int processMSQLTrx( Reader reader) throws Exception {
239 return processMSQLTrx( MobisnapConstants.MSQL_SERVER, reader);
240 }
241 public synchronized int processMSQLTrx( int msql_type, Reader reader) throws Exception {
242 initTransaction( reader);
243 SimpleNode mainNode = MobisnapSQL.CompilationUnit() ;
244 if( msql_type == MobisnapConstants.MSQL_RESERVATION_CLIENT) {
245 try {
246 initReservations();
247 mainNode.process( msql_type);
248 throw new mobisnap.MobisnapException( "Internal error - compilation unit ended without a result");
249 } catch( CommitException e) {
250 commitReservations();
251 return MobisnapConstants.MSQLCR_RESERVATION_COMMIT;
252 } catch( UnknownException e) {
253 transaction.rollback();
254 return MobisnapConstants.MSQLCR_UNKNOWN;
255 } catch( RollbackException e) {
256 // do nothing
257 } catch( NoReservationException e) {
258 // do nothing
259 } finally {
260 rollbackReservations();
261 }
262 msql_type = MobisnapConstants.MSQL_TENTATIVE_CLIENT;
263 }
264 if( msql_type == MobisnapConstants.MSQL_SERVER) {
265 try {
266 mainNode.process( msql_type);
267 throw new mobisnap.MobisnapException( "Internal error - compilation unit ended without a result");
268 } catch( CommitException e) {
269 return MobisnapConstants.MSQLSR_COMMIT;
270 } catch( RollbackException e) {
271 return MobisnapConstants.MSQLSR_ABORT;
272 } catch( UnknownException e) {
273 transaction.rollback();
274 return MobisnapConstants.MSQLSR_ABORT;
275 }
276 } else {
277 try {
278 mainNode.process( msql_type);
279 throw new mobisnap.MobisnapException( "Internal error - compilation unit ended without a result");
280 } catch( CommitException e) {
281 return MobisnapConstants.MSQLCR_TENTATIVE_COMMIT;
282 } catch( RollbackException e) {
283 return MobisnapConstants.MSQLCR_TENTATIVE_ABORT;
284 } catch( UnknownException e) {
285 transaction.rollback();
286 return MobisnapConstants.MSQLCR_UNKNOWN;
287 }
288 }
289 }
290
291 /***
292 * Process a database definition : syntax : begin (sql_ddl_statements)* end ;
293 */
294 public synchronized void processDatabaseDef( Reader reader, Hashtable table) throws Exception {
295 initQuery( reader);
296 ASTDatabaseDefinition node = databaseDefinition();
297 Iterator it = node.definitions().iterator();
298 while( it.hasNext()) {
299 SimpleNode aux = (SimpleNode)it.next();
300 if( aux instanceof ASTCreateTable) {
301 ASTCreateTable ct = (ASTCreateTable)aux;
302 StringBuffer buffer = new StringBuffer();
303 ct.sourceCode( MobisnapConstants.MSQL_SERVER, buffer);
304 table.put( ct.tableName( MobisnapConstants.MSQL_SERVER), buffer.toString().trim());
305 } else
306 System.err.println( "instruction not processed");
307 }
308 }
309
310 public static void main( String[] args) {
311 try {
312 MobisnapInterpreter interpreter = null;
313 try {
314 System.out.println( "Usage:\nMobisnapInterpreter filename [url user pwd [driver]]");
315 if( args.length == 5)
316 interpreter = new MobisnapInterpreter( args[4], args[1], args[2], args[3]);
317 else if( args.length == 4)
318 interpreter = new MobisnapInterpreter( "sun.jdbc.odbc.JdbcOdbcDriver", args[1], args[2], args[3]);
319 else
320 interpreter = new MobisnapInterpreter( "sun.jdbc.odbc.JdbcOdbcDriver", "jdbc:odbc:wine", "nmp", "");
321
322 interpreter.processMSQLTrx( MobisnapConstants.MSQL_SERVER, new FileReader( args[0]));
323
324 } finally {
325 interpreter.release();
326 }
327 } catch( Exception e) {
328 e.printStackTrace();
329 }
330 }
331 }
This page was automatically generated by Maven